package hn.cch.spring_boot_security_rest.advice;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.MethodParameter;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;

/**
 * @Controller修饰类 在
 * @RestController修饰类
 * @ResponseBody修饰方法
 */
@ControllerAdvice(basePackages = "hn.cch.spring_boot_security_rest")
public class ApiResponseBodyAdvice implements ResponseBodyAdvice<Object> {
    private static Logger logger = LoggerFactory.getLogger(ApiResponseBodyAdvice.class);


    @Override
    public boolean supports(MethodParameter methodParameter, Class<? extends HttpMessageConverter<?>> aClass) {
        return true;
    }

    @Override
    public Object beforeBodyWrite(Object object, MethodParameter methodParameter, MediaType mediaType, Class<? extends HttpMessageConverter<?>> aClass,
                                  ServerHttpRequest serverHttpRequest, ServerHttpResponse serverHttpResponse) {
        logger.info("beforeBodyWrite");
//        Response<Object> response = new Response<>();
//        if (object == null) {
//            return response;
//        }
//
//        if (object instanceof String){
//            List<String> list = new ArrayList<>();
//            list.add((String) object);
//            response.setData(list);
//        } else if (object instanceof ResponseEntity) {
//            ResponseEntity responseEntity = (ResponseEntity) object;
//            response.setData(responseEntity.getBody());
//            response.setCode(String.valueOf(responseEntity.getStatusCodeValue()));
//            response.setInfo(responseEntity.getStatusCode().getReasonPhrase());
//
//        } else {
//            response.setData(object);
//        }
        // return JsonUtil.toStringJackson(response);
//        return response;
        return object;
    }
}
